package vp.remedies.dr.home.adapter;

import java.util.ArrayList;
import java.util.List;

import vp.remedies.dr.home.R;
import vp.remedies.dr.home.so.NutrientPropertiesWrapper;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

// The standard nutrientPropertiesTxt view adapter only seems to search from the beginning of whole words
// so we've had to write this whole class to make it possible to search
// for parts of the arbitrary string we want
public class SearchableAdapter extends BaseAdapter implements Filterable {
	private List<String> originalData = null;
	private List<String> filteredData = null;
	private LayoutInflater mInflater;
	private ItemFilter mFilter = new ItemFilter();
	private NutrientPropertiesWrapper nutrientPropertiesWrapper;

	public SearchableAdapter(Context context,
			NutrientPropertiesWrapper nutrientPropertiesWrapper) {
		this.nutrientPropertiesWrapper = nutrientPropertiesWrapper;
		this.filteredData = nutrientPropertiesWrapper.getGoodForList();
		this.originalData = nutrientPropertiesWrapper.getFoodNameList();
		mInflater = LayoutInflater.from(context);
	}

	public int getCount() {
		return filteredData.size();
	}

	public Object getItem(int position) {
		return originalData.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		// A ViewHolder keeps references to children views to avoid unnecessary
		// calls
		// to findViewById() on each row.
		ViewHolder holder;

		// When convertView is not null, we can reuse it directly, there is no
		// need
		// to reinflate it. We only inflate a new View when the convertView
		// supplied
		// by ListView is null.
		if (convertView == null) {
			convertView = mInflater.inflate(
					R.layout.nutrient_properties_list_item, null);

			// Creates a ViewHolder and store references to the two children
			// views
			// we want to bind data to.
			holder = new ViewHolder();
			holder.foodNameTxt = (TextView) convertView
					.findViewById(R.id.foodNameTxt);
			holder.goodForTxt = (TextView) convertView
					.findViewById(R.id.goodForTxt);
			holder.thumb_image = (ImageView) convertView
					.findViewById(R.id.list_image);

			// Bind the data efficiently with the holder.

			convertView.setTag(holder);
		} else {
			// Get the ViewHolder back to get fast access to the TextView
			// and the ImageView.
			holder = (ViewHolder) convertView.getTag();
		}

		// If weren't re-ordering this you could rely on what you set last time
		holder.foodNameTxt.setText(originalData.get(position));
		holder.goodForTxt.setText(filteredData.get(position));
		holder.thumb_image.setImageResource(R.drawable.dadimakenuskhe);

		return convertView;
	}

	static class ViewHolder {
		TextView foodNameTxt;
		TextView goodForTxt;
		ImageView thumb_image;
	}

	public Filter getFilter() {
		return mFilter;
	}

	private class ItemFilter extends Filter {
		@Override
		protected FilterResults performFiltering(CharSequence constraint) {
			String filterString = constraint.toString().toLowerCase();
			FilterResults results = new FilterResults();
			final List<String> list = nutrientPropertiesWrapper.getGoodForList();

			int count = list.size();
			final ArrayList<String> nlist = new ArrayList<String>(count);

			String filterableString;
			for (int i = 0; i < count; i++) {
				filterableString = list.get(i);
				if (filterableString.toLowerCase().contains(filterString)) {
					nlist.add(filterableString);
				}
			}
			results.values = nlist;
			results.count = nlist.size();

			return results;
		}

		@SuppressWarnings("unchecked")
		@Override
		protected void publishResults(CharSequence constraint,
				FilterResults results) {
			filteredData = (ArrayList<String>) results.values;
			notifyDataSetChanged();
		}

	}
}